package com.hx.springboot1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springboot1.dao.RoleDao;
import com.hx.springboot1.pojo.dto.ScoreDTO;
import com.hx.springboot1.pojo.entity.TabRole;
import com.hx.springboot1.service.RoleService;
import com.hx.springboot1.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public TabRole selectById(Integer id){
        if (id!=null){
            TabRole tabRole = roleDao.selectById(id);
            return tabRole;
        }
        return null;
    }

    @Override
    public PageInfo selectAllRoles(String searchName, String pageNum, String pageSize) {
        List<TabRole> allRoles;
        int pn = 1;
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
        }
        //   PageHelper  要在查询之前使用
        PageHelper.startPage(pn, ps);
        if (StringUtils.isEmpty(searchName)) {
            allRoles = roleDao.selectAllRoles();
            log.info("用户{}点击roleList页面", SecurityContextHolder.getContext().getAuthentication().getName());
        } else {
            searchName = "%" + searchName + "%";
            allRoles = roleDao.selectAllRolesByName(searchName);
            log.info("{}查询了角色：{}的信息", SecurityContextHolder.getContext().getAuthentication().getName(), searchName);
        }
        PageInfo<TabRole> pageInfo = new PageInfo<>(allRoles);
        return pageInfo;
    }

    @Override
    public ResultMsg updateRoleState(Integer state, Integer roleId) {
        int effectRows = 0;
        if (state!=null&&roleId!=null){
            effectRows += roleDao.updateRoleState(state,roleId);
        }
        if (effectRows>0){
            return new ResultMsg("修改状态成功！",true);
        }
        return new ResultMsg("修改状态失败！",false);
    }

    @Override
    public ResultMsg insertOrUpdateRole(Integer roleId, String roleName) {
        int effectRows = 0;
        if (!roleName.startsWith("ROLE_")){
            return new ResultMsg("角色名格式错误！应以ROLE_开头",false);
        }
        if (roleId==null){
            /* 说明是新增 */
            effectRows += roleDao.insertRole(roleName);
        }else {/* 否则为修改 */
            effectRows += roleDao.updateRoleName(roleName,roleId);
        }
        if (effectRows>0){
            return new ResultMsg("操作成功！",true);
        }else {
            return new ResultMsg("操作失败！",false);
        }
    }

    @Override
    public List<TabRole> selectAllRoles() {
        List<TabRole> tabRoles = roleDao.selectAllRolesEffect();
        return tabRoles;
    }

}
